package de.hbrs.selectionengine.bean;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

@Stateless
public class MessageSenderBean {

	@Resource(mappedName = "jms/SelectionEngineConnectionFactory")
	private ConnectionFactory connectionFactory;

	@Resource(mappedName = "jms/SelectionEngineQueue")
	private Queue queue;

	public MessageSenderBean() {
		super();
	}

	public void sendMessageToQueue(String message) {
		
		try {
			// Create the Connection Factory
			Connection connection = connectionFactory.createConnection();

			// Create the session
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);

			// Create Message Produer
			MessageProducer producer = session.createProducer(queue);

			// Send TextMessage
			TextMessage textMessage = session.createTextMessage();
//			textMessage.setText(message);
			textMessage.setStringProperty(message.split(";")[0], message.split(";")[1]);
			producer.send(textMessage);

		} catch (JMSException e) {
			// TODO: handle exception
		}
	}

}
